//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DailyTenorLibor.h"
using namespace Cephei::QL::Indexes::Ibor;
#include <gen/QL/Currency.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Times/Period.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Indexes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::Ibor::CDailyTenorLibor::CDailyTenorLibor (String^ familyName, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ financialCenterCalendar, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h) : CIborIndex(CDailyTenorLibor::typeid)
{
    CCurrency^ _Ccurrency;
    CCalendar^ _CfinancialCenterCalendar;
    CDayCounter^ _CdayCounter;
    CYieldTermStructure^ _Ch;
    try
    {
#ifdef HANDLE
        _phDailyTenorLibor = NULL;
#endif
        std::string _familyName = (std::string)ValueHelper::Convert (familyName);
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency& _currency = static_cast<QuantLib::Currency&> (_Ccurrency->GetReference ()); 
        _CfinancialCenterCalendar = safe_cast<CCalendar^> (financialCenterCalendar);
        _CfinancialCenterCalendar->Lock();
        QuantLib::Calendar& _financialCenterCalendar = static_cast<QuantLib::Calendar&> (_CfinancialCenterCalendar->GetReference ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h))
        {
            _Ch = safe_cast<CYieldTermStructure^> (h->Value);
            _Ch->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _h = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_Ch->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppDailyTenorLibor = new boost::shared_ptr<QuantLib::DailyTenorLibor> (new QuantLib::DailyTenorLibor ( _familyName,  _settlementDays,  _currency,  _financialCenterCalendar,  _dayCounter,  _h ));
        SetIborIndex (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppDailyTenorLibor));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
        if (_CfinancialCenterCalendar != nullptr) _CfinancialCenterCalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
Cephei::QL::Indexes::Ibor::CDailyTenorLibor::CDailyTenorLibor (boost::shared_ptr<QuantLib::DailyTenorLibor>& childNative, Object^ owner) : CIborIndex(CDailyTenorLibor::typeid)
{
#ifdef HANDLE
	_phDailyTenorLibor = NULL;
#endif
	_ppDailyTenorLibor = &childNative;
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppDailyTenorLibor));
}
Cephei::QL::Indexes::Ibor::CDailyTenorLibor::CDailyTenorLibor (QuantLib::DailyTenorLibor& childNative, Object^ owner) : CIborIndex(CDailyTenorLibor::typeid)
{
#ifdef HANDLE
	_phDailyTenorLibor = NULL;
#endif
	_ppDailyTenorLibor = new boost::shared_ptr<QuantLib::DailyTenorLibor> (&childNative);
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppDailyTenorLibor));
    _DailyTenorLiborOwner = owner;
    _IborIndexOwner = owner;
}

Cephei::QL::Indexes::Ibor::CDailyTenorLibor::CDailyTenorLibor (CDailyTenorLibor^ copy) : CIborIndex(CDailyTenorLibor::typeid)
{
#ifdef HANDLE
	_phDailyTenorLibor = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDailyTenorLibor = new boost::shared_ptr<QuantLib::DailyTenorLibor> (copy->GetShared());
        _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppDailyTenorLibor));
    }
}
Cephei::QL::Indexes::Ibor::CDailyTenorLibor::CDailyTenorLibor (System::Type^ t) : CIborIndex(CDailyTenorLibor::typeid)
{
#ifdef HANDLE
	_phDailyTenorLibor = NULL;
#endif
	if (!t->IsSubclassOf(CDailyTenorLibor::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::Ibor::CDailyTenorLibor::CDailyTenorLibor (QuantLib::Handle<QuantLib::DailyTenorLibor>& childNative, Object^ owner)  : CIborIndex(CDailyTenorLibor::typeid)
{
	_phDailyTenorLibor = &childNative;
	_ppDailyTenorLibor = &static_cast<boost::shared_ptr<QuantLib::DailyTenorLibor>>(childNative.currentLink());
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppDailyTenorLibor));
    _DailyTenorLiborOwner = owner;
}
Cephei::QL::Indexes::Ibor::CDailyTenorLibor::CDailyTenorLibor (QuantLib::Handle<QuantLib::DailyTenorLibor> childNative)  : CIborIndex(CDailyTenorLibor::typeid)
{
	_phDailyTenorLibor = &childNative;
	_ppDailyTenorLibor = &static_cast<boost::shared_ptr<QuantLib::DailyTenorLibor>>(childNative.currentLink());
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppDailyTenorLibor));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::Ibor::CDailyTenorLibor::CDailyTenorLibor (QuantLib::DailyTenorLibor childNative)  : CIborIndex(CDailyTenorLibor::typeid)
{
#ifdef HANDLE
	_phDailyTenorLibor = NULL;
#endif
	_ppDailyTenorLibor = new boost::shared_ptr<QuantLib::DailyTenorLibor> (new QuantLib::DailyTenorLibor (childNative));
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppDailyTenorLibor));
}
#endif

Cephei::QL::Indexes::Ibor::CDailyTenorLibor::~CDailyTenorLibor ()
{
    if (_ppDailyTenorLibor != NULL)
    {
	    delete _ppDailyTenorLibor;
        _ppDailyTenorLibor = NULL;
    }
}
Cephei::QL::Indexes::Ibor::CDailyTenorLibor::!CDailyTenorLibor ()
{
    if (_ppDailyTenorLibor != NULL)
    {
	    delete _ppDailyTenorLibor;
    }
}
QuantLib::DailyTenorLibor& Cephei::QL::Indexes::Ibor::CDailyTenorLibor::GetReference ()
{
    if (_ppDailyTenorLibor == NULL) throw gcnew NativeNullException ();
	return **_ppDailyTenorLibor;
}
boost::shared_ptr<QuantLib::DailyTenorLibor>& Cephei::QL::Indexes::Ibor::CDailyTenorLibor::GetShared ()
{
    if (_ppDailyTenorLibor == NULL) throw gcnew NativeNullException ();
	return *_ppDailyTenorLibor;
}
QuantLib::DailyTenorLibor* Cephei::QL::Indexes::Ibor::CDailyTenorLibor::GetPointer ()
{
    if (_ppDailyTenorLibor == NULL) throw gcnew NativeNullException ();
	return &**_ppDailyTenorLibor;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DailyTenorLibor>& Cephei::QL::Indexes::Ibor::CDailyTenorLibor::GetHandle ()
{
	if (_phDailyTenorLibor == NULL)
	{
		_phDailyTenorLibor = new Handle<QuantLib::DailyTenorLibor> (*_ppDailyTenorLibor);
	}
	return *_phDailyTenorLibor;
}
#endif
bool Cephei::QL::Indexes::Ibor::CDailyTenorLibor::HasNative () 
{
	return (_ppDailyTenorLibor != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::Ibor::IDailyTenorLibor^ Cephei::QL::Indexes::Ibor::CDailyTenorLibor_Factory::Create (String^ familyName, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ financialCenterCalendar, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h)
{
    return gcnew CDailyTenorLibor ( familyName,  settlementDays,  currency,  financialCenterCalendar,  dayCounter,  h);
}
